﻿#region DaDirFile
/*----------------------------------------------------------------
// 文件名：DaDirFile.cs
// 功能描述：表dbo.Dir_File的数据访问层
//
// 
// 创建时间：2009-11-20 DtataAccess template . Ver 5.0.20090413
//
// 修改标识： 
// 修改描述： 
//----------------------------------------------------------------*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Globalization;
using System.Text;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

using EQT.Model;


namespace EQT.Dal
{
    ///可以继承更上层接口,IDirFile ,数据访问层基类DaBase，
    ///DaBase中包含了DbException异常处理静态方法
    public partial class DaDirFile : DaBase<MoDirFile>
    {
        #region SQL const
        internal const string TABLE_COLUMNS = " DF_ID ,UID ,PARENT_DID ,DF_NAME ,DF_FLAG ,REM ,CREATE_TIME   ";

        internal const string SQL_INSERT = "INSERT INTO DIR_FILE (" + TABLE_COLUMNS + ") VALUES (@DfId,@Uid,@ParentDid,@DfName,@DfFlag,@Rem,@CreateTime)";
        internal const string SQL_SELECT = "SELECT " + TABLE_COLUMNS + " FROM DIR_FILE  ";
        internal const string SQL_SELECT_ONE = SQL_SELECT + " WHERE DF_ID=@DfId";

        internal const string SQL_EXIST = "SELECT COUNT(*) FROM DIR_FILE  WHERE DF_ID=@DfId  ";
        internal const string SQL_UPDATE = "UPDATE DIR_FILE SET UID=@Uid, PARENT_DID=@ParentDid, DF_NAME=@DfName, DF_FLAG=@DfFlag, REM=@Rem, CREATE_TIME=@CreateTime WHERE DF_ID=@DfId";

        internal const string SQL_DELETE_DEFAULT = "DELETE FROM DIR_FILE ";
        internal const string SQL_DELETE = "DELETE FROM DIR_FILE WHERE DF_ID=@DfId";

        internal const string SQL_COUNT = "SELECT COUNT(*) FROM Dir_File ";
        #endregion

        #region Constructor

        /// <summary>
        /// 默认构造函数，使用配置文件中默认的数据库配置。
        /// </summary>
        public DaDirFile()
        {
            this.db = DatabaseFactory.CreateDatabase();
        }
        /// <summary>
        /// 使用指定的databaseName数据库配置。
        /// </summary>
        /// <param name="databaseName">配置文件中数据库的配置名称。</param>
        public DaDirFile(string databaseName)
        {
            this.db = DatabaseFactory.CreateDatabase(databaseName);
        }

        public DaDirFile(Database db)
        {
            this.db = db;
        }

        #endregion

        //以下是CRUD方法，每个方法都有引入外部事务的版本

        #region 构造器模块

        internal override MoDirFile ConstructT()
        {
            return new MoDirFile();
        }
        #endregion



        #region Add模块 Helper

        ///build the command object.It never throw exception.
        internal override DbCommand ConstructAddCommand(Database db)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_INSERT);

            db.AddInParameter(dbCommand, "DfId", DbType.AnsiString);
            db.AddInParameter(dbCommand, "Uid", DbType.AnsiString);
            db.AddInParameter(dbCommand, "ParentDid", DbType.AnsiString);
            db.AddInParameter(dbCommand, "DfName", DbType.String);
            db.AddInParameter(dbCommand, "DfFlag", DbType.AnsiStringFixedLength);
            db.AddInParameter(dbCommand, "Rem", DbType.String);
            db.AddInParameter(dbCommand, "CreateTime", DbType.DateTime);
            return dbCommand;
        }
        internal override DbCommand ConstructAddCommand(Database db, MoDirFile entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_INSERT);
            db.AddInParameter(dbCommand, "DfId", DbType.AnsiString, entity.DfId);
            db.AddInParameter(dbCommand, "Uid", DbType.AnsiString, entity.Uid);
            db.AddInParameter(dbCommand, "ParentDid", DbType.AnsiString, entity.ParentDid);
            db.AddInParameter(dbCommand, "DfName", DbType.String, entity.DfName);
            db.AddInParameter(dbCommand, "DfFlag", DbType.AnsiStringFixedLength, entity.DfFlag);
            db.AddInParameter(dbCommand, "Rem", DbType.String, entity.Rem);
            db.AddInParameter(dbCommand, "CreateTime", DbType.DateTime, entity.CreateTime);
            return dbCommand;
        }

        internal override DbCommand PopulateAddCommandParameters(Database db, DbCommand addCmd, MoDirFile entity)
        {
            db.SetParameterValue(addCmd, "DfId", entity.DfId);
            db.SetParameterValue(addCmd, "Uid", entity.Uid);
            db.SetParameterValue(addCmd, "ParentDid", entity.ParentDid);
            db.SetParameterValue(addCmd, "DfName", entity.DfName);
            db.SetParameterValue(addCmd, "DfFlag", entity.DfFlag);
            db.SetParameterValue(addCmd, "Rem", entity.Rem);
            db.SetParameterValue(addCmd, "CreateTime", entity.CreateTime);
            return addCmd;
        }

        internal override DbCommand PopulateAddCommandParameters(Database db, DbCommand addCmd, DataRow row)
        {
            db.SetParameterValue(addCmd, "DfId", row["DfId"]);
            db.SetParameterValue(addCmd, "Uid", row["Uid"]);
            db.SetParameterValue(addCmd, "ParentDid", row["ParentDid"]);
            db.SetParameterValue(addCmd, "DfName", row["DfName"]);
            db.SetParameterValue(addCmd, "DfFlag", row["DfFlag"]);
            db.SetParameterValue(addCmd, "Rem", row["Rem"]);
            db.SetParameterValue(addCmd, "CreateTime", row["CreateTime"]);
            return addCmd;
        }

        #endregion

        #region Update模块 Helper

        internal override DbCommand ConstructUpdateCommand(Database db)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_UPDATE);
            db.AddInParameter(dbCommand, "Uid", DbType.AnsiString);
            db.AddInParameter(dbCommand, "ParentDid", DbType.AnsiString);
            db.AddInParameter(dbCommand, "DfName", DbType.String);
            db.AddInParameter(dbCommand, "DfFlag", DbType.AnsiStringFixedLength);
            db.AddInParameter(dbCommand, "Rem", DbType.String);
            db.AddInParameter(dbCommand, "CreateTime", DbType.DateTime);
            db.AddInParameter(dbCommand, "DfId", DbType.AnsiString);

            return dbCommand;
        }
        internal override DbCommand ConstructUpdateCommand(Database db, MoDirFile entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_UPDATE);
            db.AddInParameter(dbCommand, "Uid", DbType.AnsiString, entity.Uid);
            db.AddInParameter(dbCommand, "ParentDid", DbType.AnsiString, entity.ParentDid);
            db.AddInParameter(dbCommand, "DfName", DbType.String, entity.DfName);
            db.AddInParameter(dbCommand, "DfFlag", DbType.AnsiStringFixedLength, entity.DfFlag);
            db.AddInParameter(dbCommand, "Rem", DbType.String, entity.Rem);
            db.AddInParameter(dbCommand, "CreateTime", DbType.DateTime, entity.CreateTime);
            db.AddInParameter(dbCommand, "DfId", DbType.AnsiString, entity.DfId);

            return dbCommand;
        }

        internal override DbCommand PopulateUpdateCommandParameters(Database db, DbCommand updateCmd, MoDirFile entity)
        {
            db.SetParameterValue(updateCmd, "Uid", entity.Uid);
            db.SetParameterValue(updateCmd, "ParentDid", entity.ParentDid);
            db.SetParameterValue(updateCmd, "DfName", entity.DfName);
            db.SetParameterValue(updateCmd, "DfFlag", entity.DfFlag);
            db.SetParameterValue(updateCmd, "Rem", entity.Rem);
            db.SetParameterValue(updateCmd, "CreateTime", entity.CreateTime);
            db.SetParameterValue(updateCmd, "DfId", entity.DfId);
            return updateCmd;
        }
        internal override DbCommand PopulateUpdateCommandParameters(Database db, DbCommand updateCmd, DataRow row)
        {
            db.SetParameterValue(updateCmd, "Uid", row["Uid"]);
            db.SetParameterValue(updateCmd, "ParentDid", row["ParentDid"]);
            db.SetParameterValue(updateCmd, "DfName", row["DfName"]);
            db.SetParameterValue(updateCmd, "DfFlag", row["DfFlag"]);
            db.SetParameterValue(updateCmd, "Rem", row["Rem"]);
            db.SetParameterValue(updateCmd, "CreateTime", row["CreateTime"]);
            db.SetParameterValue(updateCmd, "DfId", row["DfId"]);
            return updateCmd;
        }

        internal override void PrepareDataAdapterCommand(Database db, out DbCommand dbInsertCommand,
                             out DbCommand dbUpdateCommand, out DbCommand dbDeleteCommand)
        {
            dbInsertCommand = db.GetSqlStringCommand(SQL_INSERT);
            #region set insert cmd parameters
            db.AddInParameter(dbInsertCommand, "DfId", DbType.AnsiString, "DF_ID", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "Uid", DbType.AnsiString, "UID", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "ParentDid", DbType.AnsiString, "PARENT_DID", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "DfName", DbType.String, "DF_NAME", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "DfFlag", DbType.AnsiStringFixedLength, "DF_FLAG", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "Rem", DbType.String, "REM", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "CreateTime", DbType.DateTime, "CREATE_TIME", DataRowVersion.Current);
            #endregion
            dbUpdateCommand = db.GetSqlStringCommand(SQL_UPDATE);
            #region Set update cmd value parameters
            db.AddInParameter(dbUpdateCommand, "Uid", DbType.AnsiString, "UID", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "ParentDid", DbType.AnsiString, "PARENT_DID", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "DfName", DbType.String, "DF_NAME", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "DfFlag", DbType.AnsiStringFixedLength, "DF_FLAG", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "Rem", DbType.String, "REM", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "CreateTime", DbType.DateTime, "CREATE_TIME", DataRowVersion.Current);
            #endregion
            #region set update cmd pk where parameters
            db.AddInParameter(dbUpdateCommand, "DfId", DbType.AnsiString, "DF_ID", DataRowVersion.Current);
            #endregion
            dbDeleteCommand = db.GetSqlStringCommand(SQL_DELETE);
            #region set delete cmd pk where parameters
            db.AddInParameter(dbDeleteCommand, "DfId", DbType.AnsiString, "DF_ID", DataRowVersion.Current);
            #endregion
        }
        #endregion

        #region Delete模块 Helper

        internal override DbCommand ConstructDeleteCommand(Database db)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_DELETE);

            db.AddInParameter(dbCommand, "DfId", DbType.AnsiString);

            return dbCommand;
        }
        internal override DbCommand ConstructDeleteCommand(Database db, MoDirFile entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_DELETE);

            db.AddInParameter(dbCommand, "DfId", DbType.AnsiString, entity.DfId);

            return dbCommand;
        }

        internal override DbCommand ConstructDeleteCommandForWhere(Database db, string where)
        {
            return db.GetSqlStringCommand(SQL_DELETE_DEFAULT + where);
        }

        internal override DbCommand PopulateDeleteCommandParameters(Database db, DbCommand deleteCmd, MoDirFile entity)
        {
            db.SetParameterValue(deleteCmd, "DfId", entity.DfId);
            return deleteCmd;
        }

        internal override DbCommand PopulateDeleteCommandParameters(Database db, DbCommand deleteCmd, DataRow row)
        {
            db.SetParameterValue(deleteCmd, "DfId", row["DfId"]);
            return deleteCmd;
        }

        #endregion helper

        #region Query模块 Helper

        internal override DbCommand ConstructQueryCommand(string condition)
        {
            return db.GetSqlStringCommand(SQL_SELECT + condition);
        }

        #endregion

        #region GetEntity(s)模块 Helper

        internal override DbCommand ConstructSelectOneCommand(Database db, MoDirFile entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_SELECT_ONE);

            db.AddInParameter(dbCommand, "DfId", DbType.AnsiString, entity.DfId);
            return dbCommand;
        }

        internal override void PopulateEntityByDataReader(IDataReader reader, ref MoDirFile entity)
        {
            if (!reader.IsDBNull(0)) entity.DfId = reader.GetString(0);
            if (!reader.IsDBNull(1)) entity.Uid = reader.GetString(1);
            if (!reader.IsDBNull(2)) entity.ParentDid = reader.GetString(2);
            if (!reader.IsDBNull(3)) entity.DfName = reader.GetString(3);
            if (!reader.IsDBNull(4)) entity.DfFlag = reader.GetString(4);
            if (!reader.IsDBNull(5)) entity.Rem = reader.GetString(5);
            if (!reader.IsDBNull(6)) entity.CreateTime = reader.GetDateTime(6);
        }



        internal override DbCommand ConstructSelectConditionCommand(Database db, string condition)
        {
            return db.GetSqlStringCommand(SQL_SELECT + condition);
        }

        #endregion

        #region Count模块 Helper

        internal override DbCommand ConstructCountCommand(Database db)
        {
            return db.GetSqlStringCommand(SQL_COUNT);
        }

        internal override DbCommand ConstructCountConditionCommand(Database db, string condition)
        {
            return db.GetSqlStringCommand(SQL_COUNT + condition);
        }

        #endregion

        #region IsExist模块 Helper

        internal override DbCommand ConstructIsExistCommand(Database db, MoDirFile entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_EXIST);
            db.AddInParameter(dbCommand, "DfId", DbType.AnsiString, entity.DfId);
            return dbCommand;
        }

        #endregion



    }
}
#endregion

